/*
 DNN Content Builder - sourceforge.net/projects/dnncb
 Copyright (c) 2004-2008
 by Emerson Lopes (emersonlopes@gmail.com)

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
 to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 DEALINGS IN THE SOFTWARE.
*/

using System.Collections;
using System.Text;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Tabs;
using FunkeLab;

namespace DNNContentBuilder.UserControls
{
	using System;

	/// <summary>
	///		Summary description for ModuleTreeView.
	/// </summary>
	public class ModuleTreeView : System.Web.UI.UserControl
	{
		#region Controls
		protected FunkeLab.MyTreeView TreeView;
		#endregion

		#region Event Handlers
		private void Page_Load(object sender, System.EventArgs e)
		{
			if (!this.IsPostBack)
			{
				BuildTree();
			}
		}
		#endregion

		#region Methods
		private void BuildTree()
		{
			string path = this.ResolveUrl("~/DeskTopModules/DNNContentBuilder/images/");
			FunkeLab.MyTreeView.ImagesFolder = path;

			TreeView.Items.Clear();

			ArrayList tabs = new TabController().GetTabs(ContextUtil.GetPortalId(this));
			

			foreach (TabInfo tabInfo in tabs)
			{
				// Build the first level items and proceed from there
				if (tabInfo.Level == 0)
				{
					if (!tabInfo.IsAdminTab && !tabInfo.IsDeleted && !tabInfo.IsSuperTab) {
						MyTreeViewItem item = CreateNode(tabInfo);
						TreeView.Items.Add(item);
					}
				}
			}

			TreeView.OpenAll();
		}

		private FunkeLab.MyTreeViewItem CreateTreeNodes(int parentId, FunkeLab.MyTreeViewItem treeViewItem)
		{
			ArrayList list = new TabController().GetTabsByParentId(parentId);

			foreach (TabInfo tabInfo in list)  
			{
				if (!tabInfo.IsAdminTab && !tabInfo.IsDeleted && !tabInfo.IsSuperTab) {
					// Create treeview item
					MyTreeViewItem item = CreateNode(tabInfo);
				
					treeViewItem.Items.Add(item);
				}
			}

			return treeViewItem;
		}

		private MyTreeViewItem CreateNode(TabInfo tabInfo)
		{
			MyTreeViewItem item = CreateNode(tabInfo.TabID, GetNodeText(tabInfo));

			ArrayList list = new ModuleController().GetPortalTabModules(tabInfo.PortalID, tabInfo.TabID);

			foreach (ModuleInfo moduleInfo in list)
			{
				ArrayList labels = new LabelController().GetLabelsByModule(moduleInfo.ModuleID);
				StringBuilder sb = new StringBuilder();

				foreach (LabelInfo label in labels)
				{
					sb.AppendFormat("{0}, ", label.ShortName);
				}
				
				MyTreeViewItem node = CreateNode(moduleInfo.ModuleID, GetNodeText(moduleInfo));
				
				bool showSelector = sb.Length > 0;

				if (showSelector && !moduleInfo.IsAdmin)
				{
					node.ToolTip  = sb.ToString().Substring(0, sb.Length - 2);
					node.Selector = FunkeLab.MyTreeViewItem.SelectionMode.CheckBox;
					item.Items.Add(node);
				}
			}

			if (tabInfo.HasChildren)
			{
				item = CreateTreeNodes(tabInfo.TabID, item);
			}

			return item;
		}

		private static MyTreeViewItem CreateNode(int id, string text)
		{
			FunkeLab.MyTreeViewItem item = new FunkeLab.MyTreeViewItem();
	
			item.Value = id.ToString();
			item.Text  = text;
			
			return item;
		}

		/// <summary>
		/// Gets the title to display for a Tab node (either the tab Title or the TabName).
		/// </summary>
		/// <param name="tabInfo"></param>
		/// <returns></returns>
		private string GetNodeText(TabInfo tabInfo)
		{
			return tabInfo.Title != null && tabInfo.Title.Length > 0 ? tabInfo.Title : tabInfo.TabName;
		}

		/// <summary>
		/// Gets the title to display for a Module node (either the friendly name or the ModuleTitle).
		/// </summary>
		/// <param name="moduleInfo"></param>
		/// <returns></returns>
		private string GetNodeText(ModuleInfo moduleInfo)
		{
			return moduleInfo.FriendlyName != null && moduleInfo.FriendlyName.Length > 0 ? moduleInfo.FriendlyName : moduleInfo.ModuleTitle;
		}

		#endregion

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion
	}
}
